****FIGURE 9****

cd "$main_dir/output/figures/main"

graph set window fontface "Palatino"

global basic 	sex marital religion edu

//--------------------------------------------------------
//Read in relevant datasets
//--------------------------------------------------------

clear
use "$data_dir/clean/harmonized_dataset.dta"

merge m:1 ccode survey_year yob using "$data_dir/raw/lifeexp_variables_data/growth_rate_mpd_var_exp.dta", nogen keepusing(total_growth_rate_obs wgrowth_rate_1 yob_ln_gdppc)

local varlist v2x_polyarchy v2xel_frefair v2x_jucon v2xlg_legcon v2mecenefm v2x_freexp
foreach var of local varlist {
	merge m:1 ccode survey_year yob using "$data_dir/raw/lifeexp_variables_data/`var'_var_exp.dta", nogen keepusing(w`var'_1)
}

fegen subregion_yob = group(subregion yob) if yob!=-99
fegen svy_survey_year = group(survey_year survey)

cap drop sample
gen sample = 1 if born_country!=0 & total_growth_rate_obs>=0.9*(age-1) & total_growth_rate_obs!=.

//--------------------------------------------------------
//Create Figure 9
//--------------------------------------------------------

matrix coef1 = J(6, 3, .)
	matrix coln coef1 = coefficients lcl ucl
	matrix rown coef1 = "Electoral Democracy" "Free and Fair Elections" "Judicial Constraints" "Legislative Constraints" "Freedom of Expression" "Media Freedom" 

matrix coef2 = J(6, 3, .)
	matrix coln coef2 = coefficients lcl ucl
	matrix rown coef2 = "Electoral Democracy" "Free and Fair Elections" "Judicial Constraints" "Legislative Constraints" "Freedom of Expression" "Media Freedom" 
	
matrix coef3 = J(6, 3, .)
	matrix coln coef3 = coefficients lcl ucl
	matrix rown coef3 = "Electoral Democracy" "Free and Fair Elections" "Judicial Constraints" "Legislative Constraints" "Freedom of Expression" "Media Freedom" 

matrix coef4 = J(6, 3, .)
	matrix coln coef4 = coefficients lcl ucl
	matrix rown coef4 = "Electoral Democracy" "Free and Fair Elections" "Judicial Constraints" "Legislative Constraints" "Freedom of Expression" "Media Freedom"  

local i = 0
local varlist wv2x_polyarchy_1 wv2xel_frefair_1 wv2x_jucon_1 wv2xlg_legcon_1 wv2x_freexp_1 wv2mecenefm_1
foreach var of local varlist {
	local ++i
	
	qui reghdfe trust_govt wgrowth_rate_1 `var' yob_ln_gdppc if sample==1, absorb(ccode subregion_yob survey_year age $basic) cluster(ccode) level(90)
	
	cap drop z*
	local list trust_govt wgrowth_rate_1 yob_ln_gdppc `var'
	foreach x of local list {
		egen z`x' = std(`x') if e(sample)==1
	}
	
	qui reghdfe ztrust_govt zwgrowth_rate_1 z`var' zyob_ln_gdppc if sample==1, absorb(ccode subregion_yob survey_year age $basic) cluster(ccode) level(90)
	
	mat os = r(table)
	mat coef1[`i',1] = os[1,1]
	mat coef1[`i',2] = os[5,1]
	mat coef1[`i',3] = os[6,1]
	
	mat coef2[`i',1] = os[1,2]
	mat coef2[`i',2] = os[5,2]
	mat coef2[`i',3] = os[6,2]
	
	reghdfe ztrust_govt zwgrowth_rate_1 z`var' zyob_ln_gdppc if sample==1, absorb(ccode subregion_yob survey_year age $basic) cluster(ccode) level(95)
	
	mat os = r(table)
	mat coef3[`i',1] = os[1,1]
	mat coef3[`i',2] = os[5,1]
	mat coef3[`i',3] = os[6,1]
	
	mat coef4[`i',1] = os[1,2]
	mat coef4[`i',2] = os[5,2]
	mat coef4[`i',3] = os[6,2]
}
												   
coefplot (matrix(coef1[,1]), ci((coef1[,2] coef1[,3])) offset(-0.05) msymbol(diamond) mlcolor(navy) mfcolor(white) mlwidth(medthick) mcolor(navy) ciopts(color(navy) lwidth(*2)) label(Growth Experience)) ///
		 (matrix(coef3[,1]), ci((coef3[,2] coef3[,3])) offset(-0.05) msymbol(diamond) mlcolor(navy) mfcolor(white) mlwidth(medthick) mcolor(navy) ciopts(color(navy)) label(Growth Experience)) ///
         (matrix(coef2[,1]), ci((coef2[,2] coef2[,3])) offset(0.05) msymbol(square) mlcolor(maroon) mfcolor(white) mlwidth(medthick)  mcolor(maroon) ciopts(color(maroon) lwidth(*2)) label(Other Experience)) ///
	     (matrix(coef4[,1]), ci((coef4[,2] coef4[,3])) offset(0.05) msymbol(square) mlcolor(maroon) mfcolor(white) mlwidth(medthick)  mcolor(maroon) ciopts(color(maroon)) label(Other Experience)), ///
	     graphregion(color(white)) bgcolor(white) xline(0) legend(order(2 "Growth Experience" 6 "Institutional Experience") size(small)) xscale(range(-0.3 0.2)) xlabel(-0.3(0.1)0.2, labs(small)) ylabel(,labs(small))
graph export "figure_9.png", replace				
graph export "figure_9.tif", replace as(tif) width(3600)
